var GuideConnexion = new (function() {

	this.formAccesDistant = DOMElement;
	this.formConnexion = DOMElement;
	
	this.accesDistant = DOMElement;
	this.partFormAccesDistant = DOMElement;
	this.testConnexion = DOMElement;
	
	var $this = this;
	
	function checkTestConnexionEnable() {
		var FreeboxLinkOptions = FreeboxLinkAPI.getOptions();
		var connexion = $this.formConnexion.JSD().toObject();
		
		if (connexion.passwordFreebox.value != "") {
			$this.testConnexion.JSD().removeClass("disabled");
			$this.testConnexion.disabled = false;
		} else {
			$this.testConnexion.JSD().addClass("disabled");
			$this.testConnexion.disabled = true;
		}
		
	}
	
	function onchangeCheckboxAccesDistant() {
		
		if ($this.accesDistant.checked) {
			$this.partFormAccesDistant.JSD().removeClass("hidden");
		} else {
			$this.partFormAccesDistant.JSD().addClass("hidden");
		}
	};
	
	function saveData() {
		var FreeboxLinkOptions = FreeboxLinkAPI.getOptions();

		var accesDistant = $this.formAccesDistant.JSD().toObject();
		var connexion = $this.formConnexion.JSD().toObject();
		
		if (connexion.savePassword) {
			FreeboxLinkOptions.savePassword = true;
			FreeboxLinkOptions.passwordFreebox = connexion.passwordFreebox;
		}
		
		if (connexion.accesDistant) {
			FreeboxLinkOptions.accesDistant = true;
			FreeboxLinkOptions.accesDistantIP = accesDistant.ip;
			FreeboxLinkOptions.accesDistantPort = accesDistant.port;
		}
		FreeboxLinkOptions.saveToStorage();
	}
	
	function loadData() {
		var FreeboxLinkOptions = FreeboxLinkAPI.getOptions();
		var formC = $this.formConnexion;
		var formAD = $this.formAccesDistant;
		
		formC.savePassword.checked = FreeboxLinkOptions.savePassword;
		if (FreeboxLinkOptions.savePassword) {
			formC.passwordFreebox.value = FreeboxLinkOptions.passwordFreebox;
		}
		formC.accesDistant.checked = FreeboxLinkOptions.accesDistant;
		
		formAD.ip.value = FreeboxLinkOptions.accesDistantIP;
		formAD.port.value = FreeboxLinkOptions.accesDistantPort;
		
		onchangeCheckboxAccesDistant();
		checkTestConnexionEnable();
	}
	
	function onTestConnexion(ret, typeError) {
		JSD.$("freebox-testing").addClass("hidden");
		var connexion = $this.formConnexion.JSD().toObject();
		console.log("Ret !");
		if (ret) {
			JSD.$("freebox-connected").removeClass("hidden");
		} else if (typeError) {
			JSD.$("freebox-not-connected").removeClass("hidden");
			if (connexion.accesDistant) {
				JSD.$("freebox-not-connected-distant").removeClass("hidden");
			} else {
				JSD.$("freebox-not-connected-local").removeClass("hidden");
			}
		} else {
			JSD.$("freebox-wrong-password").removeClass("hidden");
		}
	}
	
	function testConnexion() {
		saveData();
		JSD.$("freebox-testing").removeClass("hidden");
		JSD.$("freebox-not-connected").addClass("hidden");
		JSD.$("freebox-not-connected-local").addClass("hidden");
		JSD.$("freebox-not-connected-distant").addClass("hidden");
		JSD.$("freebox-connected").addClass("hidden");
		JSD.$("freebox-wrong-password").addClass("hidden");
		var connexion = $this.formConnexion.JSD().toObject();
		
		var FreeboxV6 = FreeboxLinkAPI.getFreeboxV6API(true);
		
		if (connexion.accesDistant) {
			var url = FreeboxLinkAPI.getURLFreebox();
			FreeboxV6.setURL(url);
			var elems = JSD.$("freebox-not-connected-distant").getElemsByCSSRule(".url-freebox");
			elems[0].innerHTML = url;
			elems[0].href = url;
			
			if (!FreeboxLinkAPI.hasPermissionOrigin("http://*/*")) {
				FreeboxLinkAPI.askPermissionOrigin("http://*/*", function() { console.log("ici?"); FreeboxV6.connect(connexion.passwordFreebox,onTestConnexion); });
				return;
			}
		}
		
		FreeboxV6.connect(connexion.passwordFreebox,onTestConnexion);
	}
	

	this.init = function() {
		
		Page.load($this, false);
		
		JSD.on("change",$this.accesDistant, onchangeCheckboxAccesDistant);
		JSD.on("change","#savePassword", saveData);
		JSD.on("click",".btn-guide-config",saveData);
		JSD.on("keyup",$this.formConnexion.passwordFreebox, checkTestConnexionEnable);
		
		JSD.on("click","#testConnexion", testConnexion);
		
		
		loadData();
		
	}
	
	this.init();
})